﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UCLibrary
{ 
    public class Function
    {
        /// <summary>
        /// 获取坐标系中某一角度圆上面得点
        /// </summary>
        /// <param name="circleCenter">圆心位置</param>
        /// <param name="circleRadius">圆的半径</param>
        /// <param name="angle">需要获取点的角度</param>
        /// <returns></returns>
        public static PointF GetCirclePoint(PointF circleCenter,float circleRadius,float angle)
        {
            //x1 = x0 + r * cos(angle * 3.14 / 180 ) 
            //y1 = y0 + r * sin(angle * 3.14 / 180)
            var x = circleCenter.X + circleRadius * Math.Cos(angle * Math.PI / 180);
            var y = circleCenter.Y + circleRadius * Math.Sin(angle * Math.PI / 180);
            return new PointF((float)x,(float)y);
        }
        public static void setHighQuality(Graphics g)
        {
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.CompositingQuality = CompositingQuality.HighQuality;

        }
        /// <summary>
        /// 颜色渐变
        /// </summary>
        /// <param name="sourceColor">渐变开始颜色</param>
        /// <param name="destColor">渐变结束颜色</param>
        /// <param name="count">渐变阶梯数</param>
        /// <returns></returns>
        public static void GradientRamp(Color sourceColor,Color destColor,int count,ref List<Color> list)
        {
            int R = destColor.R-sourceColor.R;
            int G = destColor.G - sourceColor.G;
            int B = destColor.B - sourceColor.B;
            for (int i = 0;i<count;i++)
            {
                list.Add(Color.FromArgb(sourceColor.R + i*R/count, sourceColor.G + i * G / count, sourceColor.B + i * B / count));
            }

        }
        public static Bitmap Rotate(Bitmap bmp, float angle, Color backColor)
        {
            int w = bmp.Width + 2;
            int h = bmp.Height + 2;

            PixelFormat pf;

            if (backColor == Color.Transparent)
            {
                pf = PixelFormat.Format32bppArgb;
            }
            else
            {
                pf = bmp.PixelFormat;
            }

            Bitmap tmp = new Bitmap(w, h, pf);
            Graphics g = Graphics.FromImage(tmp);
            g.Clear(backColor);
            g.DrawImageUnscaled(bmp, 1, 1);
            g.Dispose();

            GraphicsPath path = new GraphicsPath();
            path.AddRectangle(new RectangleF(0f, 0f, w, h));
            Matrix mtrx = new Matrix();
            mtrx.Rotate(angle);
            RectangleF rct = path.GetBounds(mtrx);

            Bitmap dst = new Bitmap((int)rct.Width, (int)rct.Height, pf);
            g = Graphics.FromImage(dst);
            g.Clear(backColor);
            g.TranslateTransform(-rct.X, -rct.Y);
            g.RotateTransform(angle);
            g.InterpolationMode = InterpolationMode.HighQualityBilinear;
            g.DrawImageUnscaled(tmp, 0, 0);
            g.Dispose();

            tmp.Dispose();

            return dst;
        }
        /// <summary>
        /// 根据图片计算GraphicsPath路径(低效率)
        /// </summary>
        /// <param name="img">图像资源</param>
        /// <param name="TranColor">欲透明掉的颜色</param>
        /// <returns>路径画布,已过滤掉了透明颜色</returns>
        public static GraphicsPath ImageToGraphicsPath(Image imgx, Color TranColor)
        {
            if (imgx == null) return null;
            GraphicsPath g = new GraphicsPath(FillMode.Alternate);
            Bitmap bitmap = null;
            if (typeof(Bitmap) == imgx.GetType())
                bitmap = (Bitmap)imgx;
            else
                bitmap = new Bitmap(imgx);

            int ImWidth = bitmap.Width;
            int ImHeight = bitmap.Height;
            Color curColor;
            Rectangle curRect = new Rectangle();
            curRect.Height = 1;
            bool isTransRgn;

            for (int y = 0; y < ImHeight; y++)
            {
                isTransRgn = true;
                for (int x = 0; x < ImWidth; x++)
                {
                    curColor = bitmap.GetPixel(x, y);
                    if (curColor == TranColor || x == ImWidth - 1)//如果遇到透明色或行尾
                    {
                        if (isTransRgn == false)//退出有效区
                        {
                            curRect.Width = x - curRect.X;
                            g.AddRectangle(curRect);
                        }
                    }
                    else//非透明色
                    {
                        if (isTransRgn == true)//进入有效区
                        {
                            curRect.X = x;
                            curRect.Y = y;
                        }
                    }//if curColor
                    isTransRgn = curColor == TranColor;
                }
            }
            return g;
        }
        /// <summary>
        /// 根据图片计算Region路径(低效率)
        /// </summary>
        /// <param name="img">图像资源</param>
        /// <param name="TranColor">欲透明掉的颜色</param>
        /// <returns>一个离散的路径信息</returns>
        public static Region ImageToRegion1(Image imgx, Color TranColor)
        {
            if (imgx == null) return null;
            Region rRegion = new Region();
            rRegion.MakeEmpty();

            Bitmap bitmap = null;
            if (typeof(Bitmap) == imgx.GetType())
                bitmap = (Bitmap)imgx;
            else
                bitmap = new Bitmap(imgx);

            int ImWidth = bitmap.Width;
            int ImHeight = bitmap.Height;
            Color curColor;
            Rectangle curRect = new Rectangle();
            curRect.Height = 1;
            bool isTransRgn;

            for (int y = 0; y < ImHeight; y++)
            {
                isTransRgn = true;
                for (int x = 0; x < ImWidth; x++)
                {
                    curColor = bitmap.GetPixel(x, y);
                    if (curColor == TranColor || x == ImWidth - 1)//如果遇到透明色或行尾
                    {
                        if (isTransRgn == false)//退出有效区
                        {
                            curRect.Width = x - curRect.X;
                            rRegion.Union(curRect);
                        }
                    }
                    else//非透明色
                    {
                        if (isTransRgn == true)//进入有效区
                        {
                            curRect.X = x;
                            curRect.Y = y;
                        }
                    }//if curColor
                    isTransRgn = curColor == TranColor;
                }
            }
            return rRegion;
        }
        /*
         * 为了鼓励学习研究精神,该函数,仅能用于本示例
         * 如果使用到其他项目,可能会存在错误
         * 如果您确实需要正确代码,请学习位图相关信息
        */
        /// <summary>
        /// 取得一个图片中非透明色部分的区域。
        /// </summary>
        /// <param name="Picture">取其区域的图片。</param>
        /// <param name="TransparentColor">透明色。</param>
        /// <returns>图片中非透明色部分的区域</returns>
        //public unsafe static Region ImageToRegion2(Image Picture, Color TransparentColor)
        //{
        //    if (Picture == null) return null;
        //    Region rgn = new Region();
        //    rgn.MakeEmpty();

        //    Bitmap bitmap = null;
        //    if (Picture.GetType() != typeof(Bitmap))
        //        bitmap = new Bitmap(Picture);
        //    else
        //        bitmap = (Bitmap)Picture;

        //    int width = bitmap.Width;
        //    int height = bitmap.Height;
        //    BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
        //    byte* p = (byte*)bmData.Scan0;
        //    int offset = bmData.Stride - width * 3;

        //    int p0, p1, p2;         // 记录透明色
        //    p0 = TransparentColor.R;
        //    p1 = TransparentColor.G;
        //    p2 = TransparentColor.B;

        //    Rectangle curRect = new Rectangle();
        //    curRect.Height = 1;

        //    int start = -1;
        //    // 行座标 ( Y col ) 
        //    for (int Y = 0; Y < height; Y++)
        //    {
        //        // 列座标 ( X row ) 
        //        for (int X = 0; X < width; X++)
        //        {
        //            if (start == -1 && (p[0] != p0 || p[1] != p1 || p[2] != p2))     //如果 之前的点没有不透明 且 不透明 
        //            {
        //                start = X;                            //记录这个点
        //                curRect.X = X;
        //                curRect.Y = Y;
        //            }
        //            else if (start > -1 && (p[0] == p0 && p[1] == p1 && p[2] == p2))      //如果 之前的点是不透明 且 透明
        //            {
        //                curRect.Width = X - curRect.X;
        //                rgn.Union(curRect);
        //                start = -1;
        //            }

        //            if (X == width - 1 && start > -1)        //如果 之前的点是不透明 且 是最后一个点
        //            {
        //                curRect.Width = X - curRect.X;
        //                rgn.Union(curRect);
        //                start = -1;
        //            }
        //            p += 3;//下一个内存地址
        //        }
        //        p += offset;
        //    }
        //    bitmap.UnlockBits(bmData);
        //    bitmap.Dispose();
        //    return rgn;
        //}
    }
}
